package pl.szpadel.android.gadu;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/// Creates/updates message database
public class MessageDatabaseOpenHelper extends SQLiteOpenHelper {

	// columns
	static final public String COLUMN_SENDER = "sender";
	static final public String COLUMN_RECIPIENT = "recipient";
	static final public String COLUM_TIMESTAMP = "timestamp";
	static final public String COLUMN_DELIVERED = "delivered";
	static final public String COLUMN_TEXT = "text";
	
	static final public String TABLE_MESSAGES = "messages";
	
	
	static private final int DBVERSION = 1;
	
	public MessageDatabaseOpenHelper(Context context, String name) {
		super(context, name, null, DBVERSION);
		// nothing
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String createQuery = 
			"CREATE TABLE " + TABLE_MESSAGES + " ( " +
			COLUMN_SENDER + " INTEGER, " +
			COLUMN_RECIPIENT + " INTEGER, " +
			COLUM_TIMESTAMP + " INTEGER, " +
			COLUMN_TEXT + " TEXT, " +
			COLUMN_DELIVERED + " INTEGER );";
		db.execSQL(createQuery);
		
		// create indices
		String indexOnSender = "CREATE INDEX bysender ON " + TABLE_MESSAGES + "(" + COLUMN_SENDER + ");";
		String indexOnRecipient = "CREATE INDEX byrecipient ON " + TABLE_MESSAGES + "(" + COLUMN_RECIPIENT + ");";
		String indexOnDate = "CREATE INDEX bydate ON " + TABLE_MESSAGES + "(" + COLUM_TIMESTAMP + ");";
		
		db.execSQL(indexOnSender);
		db.execSQL(indexOnRecipient);
		db.execSQL(indexOnDate);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// nothing for now as we are in the version 1
	}

}
